﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using EFERP;
using HRM.DICTIONARY.DAL;
using System;
using System.Windows.Forms;


namespace HRM.DICTIONARY.UI
{
    public partial class xucRateAdd : XtraForm
    {
        private dic_rate _obj;
        private readonly ICommon<dic_rate> _exeDic;
        private readonly ICommon<dic_group_rate> _exeDicGroupRate;
        private CloseOrNew _closeNew = CloseOrNew.Close;

        public xucRateAdd(ICommon<dic_rate> exeDic)
        {
            InitializeComponent();
            this._exeDicGroupRate = new Common<dic_group_rate>(StaticVariable.ERPEn);
            this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Insert;
            this._obj = new dic_rate();
            SetDataIntoGridLookup();
            SetDataIntoUI();
        }

        public xucRateAdd(dic_rate currObj, ICommon<dic_rate> exeDic)
        {
            InitializeComponent();
            this._exeDicGroupRate = new Common<dic_group_rate>(StaticVariable.ERPEn); this._exeDic = exeDic;
            this._exeDic.myAction = Actions.Update;
            this._obj = currObj;
            SetDataIntoGridLookup();
            SetDataIntoUI();
        }

        private void xucRateAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            TotalClose();
        }

        private void SetDataIntoUI(){
            if (_obj == null)
                return;
            switch (_exeDic.myAction){
                case Actions.Update:
                    txtMaTieuChi.Text = _obj.ratecode;
                    txtTenTieuChi.Text = _obj.ratename;
                    txtMoTa.Text = _obj.description;
                    if (_obj.coefficient != null) calHeSo.Value = (decimal)_obj.coefficient;
                    glkNhomTieuChi.EditValue = _obj.groupratecode;
                    cbDangSuDung.Checked = Convert.ToBoolean(_obj.active);
                    

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = false;
                    break;
                case Actions.Insert:
                    _obj = new dic_rate();
                    txtMaTieuChi.Text = _exeDic.NewID("BC");
                   txtTenTieuChi.Text = "";
                    txtMoTa.Text = "";
                    cbDangSuDung.Checked = true;calHeSo.Value = 1;
                    glkNhomTieuChi.EditValue = "";

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
                case Actions.InsertMultiple:
                    _obj = new dic_rate();
                    txtMaTieuChi.Text = _exeDic.NewID("BC");
                   txtTenTieuChi.Text = "";
                    txtMoTa.Text = "";
                    cbDangSuDung.Checked = true;
                    calHeSo.Value = 1;
                    glkNhomTieuChi.EditValue = "";

                    btnLuuDong.Visible = true;
                    btnThoat.Visible = true;
                    btnLuuThem.Visible = true;
                    break;
            }
        }

        private bool SetUIIntoData()
        {
            if (txtMaTieuChi.Text == "" || txtTenTieuChi.Text == "" || glkNhomTieuChi.EditValue.ToString() =="")
            {
                MessageBox.Show(@"Vui lòng không để trống thông tin tiêu chí");
               
                return false;
            }
            _obj.ratecode = txtMaTieuChi.Text;
            _obj.ratename = txtTenTieuChi.Text;
            _obj.description = txtMoTa.Text;
            _obj.coefficient = (double?)calHeSo.Value;
            _obj.groupratecode = glkNhomTieuChi.EditValue.ToString();
        

            return true;
        }

         //Add them if we have to combine the other tables to get data
        private bool SetDataIntoGridLookup()
        {
            bool kq = SetDataSysBrandIntoGridLookup();
            if (kq) return true;
            XtraMessageBox.Show(_exeDicGroupRate.ErrMsg, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return false;
        }

        private bool SetDataSysBrandIntoGridLookup()
        {
            bool kq = _exeDicGroupRate.AddGridLookupEdit(glkNhomTieuChi, "groupratename", "groupratecode");
            if (!kq) return false;
            //if (glkNhomTieuChi.Properties.DataSource != null)
            //{
            //    glkNhomTieuChi.EditValue = @"";}
            return true;
        }

        private void TotalControl()
        {
            bool kq;
            switch (_exeDic.myAction)
            {
                case Actions.Insert:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Insert;
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;

                case Actions.Update:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.UpdateOnGridView(_obj);
                        if (kq)
                        {
                            this.DialogResult = DialogResult.OK;
                            _exeDic.myAction = Actions.Update;
                            this.Close();
                        }
                        else
                        {
                            _exeDic.ValueOfGridViewRow = null;
                            MessageBox.Show(@"Lỗi cập nhật dữ liệu vào hệ thống");
                        }
                    }
                    break;
                case Actions.InsertMultiple:
                    kq = SetUIIntoData();
                    if (kq)
                    {
                        kq = _exeDic.InsertOnGridView(_obj);
                        if (kq)
                        {
                            _exeDic.myAction = Actions.InsertMultiple;
                            SetDataIntoUI();
                            this.DialogResult = DialogResult.OK;
                            if (_closeNew == CloseOrNew.Close)
                                this.Close();
                            else
                                _closeNew = CloseOrNew.New;
                        }
                        else
                        {
                            MessageBox.Show(@"Lỗi thêm dữ liệu vào hệ thống");
                        }
                    }
                    break;
            }
        }
        private void TotalClose()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            TotalClose();
        }

        private void btnLuuDong_Click(object sender, EventArgs e)
        {
            _closeNew = CloseOrNew.Close;
            TotalControl();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            _exeDic.myAction = Actions.InsertMultiple;
            _closeNew = CloseOrNew.New;
            TotalControl();
        }

        private void glkNhomTieuChi_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind != ButtonPredefines.Plus) return; xucGroupRateAdd add = new xucGroupRateAdd(_exeDicGroupRate);
            add.ShowDialog();
        }
    }
}
